package com.atguigu.controller;

import com.atguigu.client.ProductFeignClient;
import com.atguigu.client.SearchFeignClient;
import com.atguigu.result.RetVal;
import com.atguigu.search.SearchParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class IndexController {
    @Autowired
    ProductFeignClient productFeignClient;
    @Autowired
    SearchFeignClient searchFeignClient;

    @GetMapping({"/","index.html"})
    public String index(Model model){
        RetVal indexCategoryInfo = productFeignClient.getIndexCategoryInfo();
        model.addAttribute("list",indexCategoryInfo.getData());
        return "index/index";
    }

    //商品的搜索功能
    @GetMapping("search.html")
    public String searchProduct(SearchParam searchParam,Model model){
        //通过远程调用shop-search微服务
        RetVal<Map> retVal = searchFeignClient.searchProduct(searchParam);
        //页面设置搜索到的商品信息
        model.addAllAttributes(retVal.getData());
        //前台需要存储一个searchParam
        model.addAttribute("searchParam",searchParam);
        //1.设置搜索路径信息
        String urlParam = pageUrlParam(searchParam);
        model.addAttribute("urlParam",urlParam);
        //2.设置品牌显示
        String brandName = pageBrandName(searchParam.getBrandName());
        model.addAttribute("brandNameParam",brandName);
        //3.设置平台属性显示
        List<Map<String, String>> propsList = pageProps(searchParam.getProps());
        model.addAttribute("propsParamList",propsList);
        //4.设置排序信息显示
        Map<String, Object> map = pageSortInfo(searchParam.getOrder());
        model.addAttribute("orderMap",map);
        return "search/index";

    }

    private String pageUrlParam(SearchParam searchParam) {
        StringBuilder urlParam = new StringBuilder();
        //判断是否通过关键字检索
        if (searchParam.getKeyword()!=null){
            urlParam.append("keyword=").append(searchParam.getKeyword());
        }

        //判断是否通过分类Id检索
        if(searchParam.getCategory1Id()!=null){
            urlParam.append("category1Id=").append(searchParam.getCategory1Id());
        }
        if(searchParam.getCategory2Id()!=null){
            urlParam.append("category2Id=").append(searchParam.getCategory2Id());
        }
        if(searchParam.getCategory3Id()!=null){
            urlParam.append("category3Id=").append(searchParam.getCategory3Id());
        }

        //判断是否通过品牌检索
        if(searchParam.getBrandName()!=null){
            if (urlParam.length()>0){
                //在原有页面参数上拼接品牌查询条件
                urlParam.append("&brandName=").append(searchParam.getBrandName());
            }
        }
        //判断是否通过平台属性检索
        if (searchParam.getProps()!=null){
            for (String prop : searchParam.getProps()) {
                //props=23:4G:运行内存
                if (urlParam.length()>0){
                    //在原有页面参数上拼接平台属性条件
                    urlParam.append("&props=").append(prop);
                }
            }
        }
        return "search.html?"+urlParam.toString();
    }
    //2.获取品牌显示
    private String pageBrandName(String brandname){
        //brandname=2:三星
        if (!StringUtils.isEmpty(brandname)){
            String[] brandnameParams = brandname.split(":");
            if (brandnameParams!=null && brandnameParams.length==2){
                return "品牌："+ brandnameParams[1];
            }
        }
        return "";
    }
    //3.获取平台属性数据
    private List<Map<String,String>> pageProps(String[] props){
        List<Map<String,String>> list = new ArrayList<>();
        if (props!=null && props.length>0){
            for (String prop : props) {
                //prop = 23:4G:运行内存
                String[] propPrams = prop.split(":");
                if (propPrams!=null && propPrams.length==3){
                    HashMap<String, String> map = new HashMap<>();
                    map.put("propertyKeyId",propPrams[0]);
                    map.put("propertyValue",propPrams[1]);
                    map.put("propertyKey",propPrams[2]);
                    list.add(map);
                }
            }
        }
        return list;
    }
    //4.显示排序信息
    private Map<String,Object> pageSortInfo(String order){
        Map<String,Object> map = new HashMap<>();
        if (!StringUtils.isEmpty(order)){
            //order=1:asc拆分数据
            String[] orderParams = order.split(":");
            if (orderParams!=null && orderParams.length==2){
                map.put("type",orderParams[0]);
                map.put("sort",orderParams[1]);
            }
        }else {
            //给一个默认的排序规则
            map.put("type","1");
            map.put("sort","asc");
        }
        return map;

    }
}
